package org.lq.shopping.service.impl;
import java.util.List;
import java.util.Set;

import org.lq.shopping.dao.ShopUserDao;
import org.lq.shopping.dao.impl.ShopUserDaoImpl;
import org.lq.shopping.entity.ShopUser;
import org.lq.shopping.service.ShopUserService;
import org.lq.shopping.util.RedisUtil;

import com.google.gson.Gson;

import redis.clients.jedis.Jedis;

public class ShopUserServiceimpl implements ShopUserService{
    ShopUserDao dao = new ShopUserDaoImpl();
	@Override
	public ShopUser Login(String uname, String upass) {
		Jedis redis = RedisUtil.getJedis();
		List<String> ss = redis.lrange("jsUser", 0, 0);
		ShopUser su = null;
		String jsUser = ss.get(0);
		Gson gson = new Gson();
		if("".equals(jsUser)) {
			ShopUser shopUser = dao.Login(uname, upass);			
			jsUser = gson.toJson(shopUser);			
			redis.lpush("jsUser",jsUser);
			List<String> ss1 =   redis.lrange("jsUser", 0, 0);
			shopUser = gson.fromJson(ss1.get(0), ShopUser.class);
			su =shopUser;
			
		}else {
			su = gson.fromJson(jsUser, ShopUser.class);
		}		
		return su;
	}

	@Override
	public boolean Register(ShopUser user) {
		Jedis redis = RedisUtil.getJedis();
		//先清空
		redis.zremrangeByRank("mysort", 0, -1);
		Gson gson = new Gson();
		String strgson = gson.toJson(user);
		//在添加
		
		if(dao.add(user)>0) {
			redis.zadd("user",1.0, strgson);
		}
		Set<String> mysort = redis.zrange("user", 0, -1);
		
		
		return mysort.size()>0 ;
	}

	@Override
	public ShopUser findByUname(String uname) {
		Jedis redis = RedisUtil.getJedis();
		List<String> ss =   redis.lrange("jsUser", 0, 0);
		ShopUser su = null;
		String jsUser = ss.get(0);
		Gson gson = new Gson();
		if("".equals(jsUser)) {
			ShopUser shopUser = dao.findByUname(uname);			
			jsUser = gson.toJson(shopUser);			
			redis.lpush("jsUser",jsUser);
			List<String> ss1 =   redis.lrange("jsUser", 0, 0);
			shopUser = gson.fromJson(ss1.get(0), ShopUser.class);
			su =shopUser;
		}else {
			su = gson.fromJson(jsUser, ShopUser.class);
		}		
		return su;
	}

	@Override
	public ShopUser findById(int id) {
		Jedis redis = RedisUtil.getJedis();
		List<String> ss =   redis.lrange("jsUser", 0, 0);
		ShopUser su = null;
		String jsUser = ss.get(0);
		Gson gson = new Gson();
		System.out.println(ss);
		if("".equals(jsUser)) {
			ShopUser shopUser = dao.findById(id);		
			jsUser = gson.toJson(shopUser);			
			redis.lpush("jsUser",jsUser);
			List<String> ss1 =   redis.lrange("jsUser", 0, 0);
			shopUser = gson.fromJson(ss1.get(0), ShopUser.class);
			su =shopUser;
			   
		}else {
			su = gson.fromJson(jsUser, ShopUser.class);	
		}		
		return su;
	}

	@Override
	public boolean updateUserInfo(ShopUser user) {
		Set<String> mysort = null;
		//获取Redis连接对象
		Jedis redis = RedisUtil.getJedis();
		//先清空
		redis.zremrangeByRank("user", 0, -1);
		Gson gson = new Gson();
		String strgson = gson.toJson(user);
		if(dao.update(user)>0) {
			//在添加
			redis.zadd("user",1.0, strgson);
			mysort = redis.zrange("user", 0, -1);
		}
		return mysort.size()>0 ;
		
	}

	/**
	 * 忘记密码时向邮箱发送验证码,并返回该验证码
	 * @param email
	 * @return 验证码
	 */
	@Override
	public String sendEmailCode(String email) {
		// 获取验证码未实现 
		return null;
	}

	/**
	 * 检验验证码
	 */
	@Override
	public boolean checkCode(String userReg) {
		//检验用户输入的验证码与收到的验证码是否一样   未实现
		return false;
	}

}
